package com.woniuxy.test_map.my_hashset;

/**
 * description: SxHashSet <br>
 * date: 2022/2/4 3:15 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

import java.util.HashMap;

/**
 * 简单的自定义HashSet底层实现
 */
public class SxHashSet {

    HashMap map;

    private static final Object PRESENT = new Object();

    public SxHashSet() {
        map = new HashMap();
    }

    public int size() {
        return map.size();
    }

    public void add(Object o) {
        map.put(o, PRESENT);
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (Object key : map.keySet()) {
            sb.append(key + ",");
        }
        sb.setCharAt(sb.length() - 1, ']');
        return sb.toString();
    }

    public static void main(String[] args) {

        SxHashSet sxHashSet = new SxHashSet();
        sxHashSet.add("a");
        sxHashSet.add("bbbb");
        sxHashSet.add("ccccc");
        sxHashSet.add("a");
        System.out.println(sxHashSet);
        System.out.println(sxHashSet.size());
    }
}
